package com.lk.base.utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class ClassUtil {

	public static String getFieldValue(Object obj,String fieldName) {
		 Class<?> aClass = obj.getClass();
        //得到属性
        Field field = null;
        try {
            field = aClass.getDeclaredField(fieldName);
            //打开私有访问
            field.setAccessible(true);
            //获取属性
            String name = field.getName();
            //获取属性值
            String subjectType = String.valueOf(field.get(obj));
            System.out.println(subjectType);
            return subjectType;

        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
	}

	public static List<String> getFieldNames(Object obj) {
		List<String> fieldNames = new ArrayList<String>();
		if (obj == null) return null;
		Field[] fields = obj.getClass().getDeclaredFields();
		for (int j = 0; j < fields.length; j++) {
			fields[j].setAccessible(true);
			// 字段名
			fieldNames.add(fields[j].getName());

		}
		return fieldNames;
	}
}
